package com.hmall.item.web;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    // 分页查询商品
    @GetMapping("/list")
    public PageDTO<Item> getItemList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {
        return itemService.getItemList(page, size);
    }

    // 根据id查询商品
    @GetMapping("/{id}")
    public Item getItemById(@PathVariable Long id){
        return itemService.getById(id);
    }

    // 新增商品
    @PostMapping
    public boolean saveItem(@RequestBody Item item){
        return itemService.save(item);
    }

    //商品上下架
    @PutMapping("/status/{id}/{tag}")
    public boolean closeOrOpenStatus(@PathVariable Long id,@PathVariable Integer tag){

        return itemService.updateStatus(id,tag);
    }

    //修改商品
    @PutMapping
    public boolean updateItem(@RequestBody Item item){
        return itemService.updateById(item);
    }

    //根据id删除商品
    @DeleteMapping("/{id}")
    public boolean deleteItem(@PathVariable Long id){
        return itemService.removeById(id);
    }


    //减库存接口
    @PutMapping("/{id}/{num}")
    public boolean updateStock(@PathVariable Long id,@PathVariable Integer num){
        return itemService.updateStock(id,num);
    }


}
